package com.woniu.order.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniu.bean.R;
import com.woniu.order.bean.*;
import com.woniu.order.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yellowDocter
 * @since 2022-08-09
 */
@CrossOrigin
@RestController
@RequestMapping("/tAddrinfo")
public class TAddrinfoController {
    @Autowired
    private ITOrderService itOrderService;
    @Autowired
    private ITAddrinfoService itAddrinfoService;
    @Autowired
    private IFAddrinfoService ifAddrinfoService;
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private IAreaService iAreaService;

    @GetMapping("getcity")
    public R getCity(String province){
        R r= null;
        System.out.println("province = " + province);
        QueryWrapper qw=new QueryWrapper();
        qw.eq("areaname",province);
        Area pro = iAreaService.getOne(qw);
        String s = pro.getAreaid().toString();
        String charSequence = (String) s.subSequence(0, 2);
        Integer i = Integer.parseInt(charSequence);

        QueryWrapper qw2=new QueryWrapper();
        qw2.eq("arealevel",2);
        qw2.likeRight("areaid",i);
        List<Area> citys = iAreaService.list(qw2);
        r=R.OK();
        Map map=new HashMap<>();
        map.put("citys", citys);
        r.setData(map);
        return r;
    }
    @GetMapping("getdistricts")
    public R getDistricts(String province,String city){
        R r= null;
        QueryWrapper qw1=new QueryWrapper();
        qw1.eq("areaname",province);
        Area pro1 = iAreaService.getOne(qw1);
        String s1 = pro1.getAreaid().toString();
        String charSequence1 = (String) s1.subSequence(0, 2);
        Integer i1 = Integer.parseInt(charSequence1);
        System.out.println("city = " + city);
        QueryWrapper qw=new QueryWrapper();
        qw.eq("areaname",city);
        qw.likeRight("areaid",i1);
        Area pro = iAreaService.getOne(qw);
        String s = pro.getAreaid().toString();
        String charSequence = (String) s.subSequence(0, 4);
        Integer i = Integer.parseInt(charSequence);

        QueryWrapper qw3=new QueryWrapper();
        qw3.eq("arealevel",3);
        qw3.likeRight("areaid",i);
        List<Area> districts = iAreaService.list(qw3);
        r=R.OK();
        Map map=new HashMap<>();
        map.put("districts", districts);
        r.setData(map);
        return r;
    }
    @GetMapping("getaddr")
    public R getaddr() throws JsonProcessingException {
        R r= null;
        QueryWrapper qw=new QueryWrapper();
        qw.eq("arealevel",1);
        List<Area> provinces = iAreaService.list(qw);
        r=R.OK();
        Map map=new HashMap<>();
        map.put("provinces", provinces);
        r.setData(map);
        return r;
    }

    @PostMapping
    public R add(@RequestBody Address address) {
        System.out.println("TAddrinfoController.add");
        System.out.println("address = " + address);

        String addressKey = "address:"+ address.gettAddrinfo().getOrderId() + ":save";
        String s = stringRedisTemplate.opsForValue().get(addressKey);

            String s1 = null;
            try {
                s1 = objectMapper.writeValueAsString(address);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            stringRedisTemplate.opsForValue().set(addressKey,s1);
            R r=null;
            if(StringUtils.hasText(stringRedisTemplate.opsForValue().get(addressKey))){
                r=R.OK();
            } else {
                r=R.ERROR();
            }
            return r;
    }
    @GetMapping
    public R load() throws JsonProcessingException {

        R r= null;
        List<TOrder> list = itOrderService.list();
        Integer orderId = list.get(list.size() - 1).getOrderId();
        orderId+=1;
        String addressKey = "address:"+ orderId + ":save";
        String s = stringRedisTemplate.opsForValue().get(addressKey);
        if(StringUtils.hasText(s)){
            Address address = objectMapper.readValue(s, Address.class);
            r=R.OK();
            Map map=new HashMap<>();
            map.put("address", address);
            r.setData(map);
            return r;
        }
        
        return r;
    }
}
